package com.columboid.testharness.main;

import java.util.UUID;

import com.columboid.protocol.syncml.representation.SyncML;
import com.columboid.testharness.util.Log4j;
import com.columboid.testharness.util.SyncMLHandler;

public class Login {

	public Login() {
	}

	public boolean userLoginChallenge(String username, String password,
			String server) {
		boolean re = false;

		try {
			String SERVER_URL = "http://" + server + "/RoutingServlet";
			Connection conn = new HttpConnection();

			// request#1
			// create new token
			String nonce = UUID.randomUUID().toString();
			String rtStr = conn.postXmlRequest(SERVER_URL, SyncMLHandler
					.CreateSymcMLObjectString("1", "1", username, null, null,
							null, nonce));
			// response#1
			if (conn.getPostSatus() == 200) {

				SyncML sml = conn.getXmlResponse(rtStr);
				// check challenge
				if (SyncMLHandler.getCredData(sml) == null
						|| !SyncMLHandler.checkCred(SyncMLHandler
								.getCredData(sml), username, password, nonce)) {
					Log4j.logger.info("Server Response 1 Error!");
					return false;
				}

				if (SyncMLHandler.getStatus(sml) != null
						&& (SyncMLHandler.getStatus(sml).equals(
								SYNCML_STATUS_CREDMISSING) || SyncMLHandler
								.getStatus(sml).equals(
										SYNCML_STATUS_ACCESSDENIED))) {
					String token = SyncMLHandler.getNextNonce(sml);

					// request#2
					// create new token
					nonce = UUID.randomUUID().toString();
					rtStr = conn.postXmlRequest(SERVER_URL, SyncMLHandler
							.CreateSymcMLObjectString("2", "1", username,
									password, token, null, nonce));

					// response#2
					if (conn.getPostSatus() == 200) {

						sml = conn.getXmlResponse(rtStr);

						// check challenge
						if (SyncMLHandler.getCredData(sml) == null
								|| !SyncMLHandler.checkCred(SyncMLHandler
										.getCredData(sml), username, password,
										nonce)) {
							Log4j.logger.info("Server Response 2 Error!");
							return false;
						}

						if (SyncMLHandler.getStatus(sml) != null
								&& SyncMLHandler.getStatus(sml).equals(
										SYNCML_STATUS_OK)) {
							re = true;

						} else if (SyncMLHandler.getStatus(sml) != null
								&& SyncMLHandler.getStatus(sml).equals(
										SYNCML_STATUS_PUBLICKEYNOTCREAED)) {
							token = SyncMLHandler.getNextNonce(sml);

							// request#3
							// create new token
							nonce = UUID.randomUUID().toString();

							rtStr = conn.postXmlRequest(SERVER_URL,
									SyncMLHandler.CreateSymcMLObjectString("3",
											"1", username, password, token,
											"PublicKey", nonce));
							// response#3
							if (conn.getPostSatus() == 200) {
								sml = conn.getXmlResponse(rtStr);

								// check challenge
								if (SyncMLHandler.getCredData(sml) == null
										|| !SyncMLHandler.checkCred(
												SyncMLHandler.getCredData(sml),
												username, password, nonce)) {
									Log4j.logger
											.info("Server Response 3 Error!");
									return false;
								}

								if (SyncMLHandler.getStatus(sml) != null
										&& SyncMLHandler.getStatus(sml).equals(
												SYNCML_STATUS_OK)) {
									re = true;

								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log4j.logger.error(e.toString());
		}
		return re;
	}

	public boolean userLogin(String username, String password, String server) {
		boolean re = false;

		try {
			String SERVER_URL = "http://" + server + "/RoutingServlet";
			Connection conn = new HttpConnection();
			// request#1
			String rtStr = conn.postXmlRequest(SERVER_URL, SyncMLHandler
					.CreateSymcMLObjectString("1", "1", username, null, null,
							null, null));
			// response#1
			if (conn.getPostSatus() == 200) {

				SyncML sml = conn.getXmlResponse(rtStr);

				if (SyncMLHandler.getStatus(sml) != null
						&& SyncMLHandler.getStatus(sml).equals(
								SYNCML_STATUS_CREDMISSING)) {
					String token = SyncMLHandler.getNextNonce(sml);
					// request#2
					rtStr = conn.postXmlRequest(SERVER_URL, SyncMLHandler
							.CreateSymcMLObjectString("2", "1", username,
									password, token, null, null));

					// response#2
					if (conn.getPostSatus() == 200) {

						sml = conn.getXmlResponse(rtStr);

						if (SyncMLHandler.getStatus(sml) != null
								&& SyncMLHandler.getStatus(sml).equals(
										SYNCML_STATUS_OK)) {
							re = true;

						} else if (SyncMLHandler.getStatus(sml) != null
								&& SyncMLHandler.getStatus(sml).equals(
										SYNCML_STATUS_PUBLICKEYNOTCREAED)) {
							token = SyncMLHandler.getNextNonce(sml);

							// request#3
							rtStr = conn.postXmlRequest(SERVER_URL,
									SyncMLHandler.CreateSymcMLObjectString("3",
											"1", username, password, token,
											"PublicKey", null));
							// response#3
							if (conn.getPostSatus() == 200) {
								sml = conn.getXmlResponse(rtStr);

								if (SyncMLHandler.getStatus(sml) != null
										&& SyncMLHandler.getStatus(sml).equals(
												SYNCML_STATUS_OK)) {
									re = true;

								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log4j.logger.error(e.toString());
		}
		return re;
	}

	public String getReturnStr() {
		return this.returnStr;
	}

	public void setReturnStr(String re) {
		this.returnStr = re;
	}

	private String returnStr = "";

	private final static String SYNCML_STATUS_OK = "200";
	private final static String SYNCML_STATUS_CREDMISSING = "407";
	@SuppressWarnings("unused")
	private final static String SYNCML_STATUS_ACCESSDENIED = "401";
	private final static String SYNCML_STATUS_PUBLICKEYNOTCREAED = "417";
	@SuppressWarnings("unused")
	private final static String SYNCML_STATUS_AUTHACCEPTED = "212";
	@SuppressWarnings("unused")
	private final static String SYNCML_STATUS_SERVERERR = "500";
}
